/**
  ******************************************************************************
  * @file           : sd_opera.c
  * @author         : Kevin_WWW
  * @brief          : None
  * @attention      : None
  * @date           : 2024/5/5
  ******************************************************************************
  */
#include "sd_opera.h"

void SDCard_ShowInfo() {
    HAL_SD_CardInfoTypeDef cardInfo;
    HAL_StatusTypeDef res = HAL_SD_GetCardInfo(&hsd, &cardInfo);
    if (res != HAL_OK) {
        lcd_show_str(10, 10 + 7 * 15, 12, "HAL_SD_GetCardInfo() error", RED);
        return;
    }

    lcd_show_str(10, 10 + 7 * 15, 12, "*** HAL_SD_GetCardInfo() info ***", RED);

    uint8_t temp_str[40];
    sprintf(temp_str, "Card Type=%d", cardInfo.CardType);
    lcd_show_str(10, 10 + 8 * 15, 12, temp_str, RED);

    sprintf(temp_str, "Card Version=%d", cardInfo.CardVersion);
    lcd_show_str(10, 10 + 9 * 15, 12, temp_str, RED);

    sprintf(temp_str, "Card Class=%d", cardInfo.Class);
    lcd_show_str(10, 10 + 10 * 15, 12, temp_str, RED);

    sprintf(temp_str, "Relative Card Address=%d", cardInfo.RelCardAdd);
    lcd_show_str(10, 10 + 11 * 15, 12, temp_str, RED);

    sprintf(temp_str, "Block Count=%d", cardInfo.BlockNbr);
    lcd_show_str(10, 10 + 12 * 15, 12, temp_str, RED);

    sprintf(temp_str, "Block Size(Bytes)=%d", cardInfo.BlockSize);
    lcd_show_str(10, 10 + 13 * 15, 12, temp_str, RED);

    sprintf(temp_str, "Logic Block Count=%d", cardInfo.LogBlockNbr);
    lcd_show_str(10, 10 + 14 * 15, 12, temp_str, RED);

    sprintf(temp_str, "Logic Block Size(Bytes)=%d", cardInfo.LogBlockSize);
    lcd_show_str(10, 10 + 15 * 15, 12, temp_str, RED);

    sprintf(temp_str, "SD Card Capacity(MB)=%d", cardInfo.BlockNbr / 1024 * cardInfo.BlockSize / 1024);
    lcd_show_str(10, 10 + 16 * 15, 12, temp_str, RED);
}

void SDCard_EraseBlocks() {
    lcd_show_str(10, 10 + 7 * 15, 12, "*** Erasing blocks ***", RED);
    uint32_t BlockStartAdd = 0;
    uint32_t BlockEndAdd = 10;
    HAL_StatusTypeDef res = HAL_SD_Erase(&hsd, BlockStartAdd, BlockEndAdd);
    if (res == HAL_OK) {
        lcd_show_str(10, 10 + 8 * 15, 12, "Erase blocks success", RED);
    } else {
        lcd_show_str(10, 10 + 8 * 15, 12, "Erase blocks error", RED);
    }
    HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);
    uint8_t temp_str[40];
    sprintf(temp_str, "GetCardState() = %d", cardState);
    lcd_show_str(10, 10 + 9 * 15, 12, temp_str, RED);
    while (cardState != HAL_SD_CARD_TRANSFER) {
        HAL_Delay(1);
        cardState = HAL_SD_GetCardState(&hsd);
    }
    sprintf(temp_str, "GetCardState() = %d", cardState);
    lcd_show_str(10, 10 + 10 * 15, 12, temp_str, RED);
    lcd_show_str(10, 10 + 11 * 15, 12, "Blocks 0-10 is erased.", BLACK);
}

void SDCard_TestWrite() {
    lcd_show_str(10, 10 + 7 * 15, 12, "*** Writing blocks ***", RED);
    uint8_t pData[BLOCKSIZE] = "Hello, welcome to UPC\0";
    uint32_t BlockAdd = 5;
    uint32_t NumberOfBlocks = 1;
    uint32_t Timeout = 1000;
    HAL_StatusTypeDef res = HAL_SD_WriteBlocks(&hsd, pData, BlockAdd, NumberOfBlocks, Timeout);
    if (res == HAL_OK) {
        lcd_show_str(10, 10 + 8 * 15, 12, "Write to Block 5 is OK", RED);
        lcd_show_str(10, 10 + 9 * 15, 12, "The string is :", RED);
        lcd_show_str(10, 10 + 10 * 15, 12, pData, RED);
    } else {
        lcd_show_str(10, 10 + 8 * 15, 12, "Write to Block 5 is error", RED);
    }
}

void SDCard_TestRead() {
    lcd_show_str(10, 10 + 7 * 15, 12, "*** Reading blocks ***", RED);
    uint8_t pData[BLOCKSIZE];
    uint32_t BlockAdd = 5;
    uint32_t NumberOfBlocks = 1;
    uint32_t Timeout = 1000;
    HAL_StatusTypeDef res = HAL_SD_ReadBlocks(&hsd, pData, BlockAdd, NumberOfBlocks, Timeout);
    if (res == HAL_OK) {
        lcd_show_str(10, 10 + 8 * 15, 12, "Read Block 5 is OK", RED);
        lcd_show_str(10, 10 + 9 * 15, 12, "The string is :", RED);
        lcd_show_str(10, 10 + 10 * 15, 12, pData, RED);
    } else {
        lcd_show_str(10, 10 + 8 * 15, 12, "Read Block 5 is error", RED);
    }
}

